package cn.com.spic.demo.service;

import cn.com.spic.demo.dao.ContractDAO;
import cn.com.spic.demo.po.ContractPO;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.iuap.baseservice.intg.support.ServiceFeature;
import com.yonyou.iuap.baseservice.service.GenericAssoService;
import com.yonyou.iuap.ucf.dao.support.SqlParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import static com.yonyou.iuap.baseservice.intg.support.ServiceFeature.AUDIT;
import static com.yonyou.iuap.baseservice.intg.support.ServiceFeature.I18N_ENUM;

/**
 * 基础CRUD及主子表联合操作服务,分离insert和update 以便RPC权限控制
 */
@Service
public class ContractService extends GenericAssoService<ContractPO, String> {

    private Logger logger = LoggerFactory.getLogger(ContractService.class);

    @Autowired
    private ContractDAO contractDAO;


    @Autowired
    public void setContractDAO(ContractDAO contractDAO) {
        this.contractDAO = contractDAO;
        super.setGenericMapper(contractDAO);
    }

    /**
     * @return 向父类 GenericIntegrateService 提供可插拔的特性声明
     * @CAU 可插拔设计
     */
    @Override
    protected ServiceFeature[] getFeats() {
        return new ServiceFeature[]{AUDIT, I18N_ENUM};
    }

    public List<ContractPO> getQueryOutOfDate(int day_count) {
        SqlParam searchParams = SqlParam.of();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -day_count);
        Date date = calendar.getTime();
        searchParams.lt("create_time", date);
        List<ContractPO> contractList = queryList(searchParams);
        return contractList;
    }

    public JSONObject getOutOfDateContent(int day_count) {
        StringBuffer sb = new StringBuffer();
        JSONObject jo = new JSONObject();
        jo.put("title", "检查合同创建日期是否超时");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss");
        String dateNow = simpleDateFormat.format(new Date());
        ;
        List<ContractPO> contractList = getQueryOutOfDate(day_count);
        try {
            if (contractList != null && contractList.size() > 0) {
                sb.append("截止到" + dateNow + ",有合同名为:");
                Iterator<ContractPO> it = contractList.iterator();
                while (it.hasNext()) {
                    ContractPO po = it.next();
                    sb.append(po.getSubject() + ",");
                }
                sb.deleteCharAt(sb.length() - 1);
                sb.append("的创建时间已超过阈值" + day_count + "天，请注意。");
                jo.put("status", "1");
                jo.put("content", sb.toString());
            } else {
                jo.put("content", "截止到" + dateNow + "，没有任何合同的创建时间超过阈值" + day_count + "天");
                jo.put("status", "1");
            }
        } catch (Exception e) {
            jo.put("content", "");
            jo.put("status", "0");
            e.printStackTrace();
        }
        return jo;
    }
}
